package com.itheima.listenter;

import cn.hutool.core.date.DateUtil;
import com.itheima.autoconfig.mq.Mqpo;
import com.itheima.constants.MqConstants;
import com.itheima.domain.db.Log;
import com.itheima.service.db.impl.LogServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

@Component
@RequiredArgsConstructor
@Slf4j
public class UserLoginListener {

    private final LogServiceImpl logService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "user.login.queue", durable = "true"),
            exchange = @Exchange(name = MqConstants.Exchange.LOGIN_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.LOGIN_KEY
    ))
    public void listenUserLoginMessage(Mqpo mqpo) {
        log.info("用户{}登录了", mqpo.getId());
        Log Log = new Log();
        Log.setUserId(mqpo.getId());
        Log.setType("0101");
        Log.setPlace("深圳");
        Log.setEquipment(mqpo.getOs());
        Log.setLogTime(String.valueOf(LocalDate.now()));

        logService.save(Log);
    }


}
